<?php

/**
 * Description of ValidatePerDiem
 *
 * @author BUI TIEN HIEP
 * @email bthiep@lhv.vn
 */
class ValidatePerDiem extends AbstractValidateFactory {
    /**
     * Constructor
     * @param type $policy
     */
    public function __construct($policy) {
        $this->policy = $policy;
    }
    /**
     * Validate Per Diem Policy template
     * @param type $expenses
     * @return boolean
     */
    public function validate($expenses = array()) {
        $bRet = false;
        $field = $this->policy['rules'][0]['field'];
        $value = $this->policy['rules'][0]['value'];
        $oprands = $this->policy['rules'][0]['$oprands'];
        $totalByCategory = 0;
        foreach ($expenses as $expense)
        {
            if ($expense[$field] == $oprands) {
                $totalByCategory+= $expense['amount'];
            }
        }
        if($totalByCategory > $value)
        {
            $bRet = true;
            $this->message = $this->policy['message'];
        }
        return $bRet;
    }
}
